extends Node2D


var list=[]
func _ready():
	Global.arrow=self
	for i in range(19):
		var sprite=Sprite.new()
		add_child(sprite)
		list.append(sprite)
		sprite.texture=load("res://Sprites/箭头1.png")
		sprite.scale=Vector2(1,1)*(0.2+float(i)/18*0.8)
		sprite.offset=Vector2(-25,0)
	var sprite=Sprite.new()
	add_child(sprite)
	list.append(sprite)
	sprite.texture=load("res://Sprites/箭头2.png")
	sprite.offset=Vector2(-25,0)
	pass

func reset(startPos,endPos):
	var ctrlAPos=Vector2()
	var ctrlBPos=Vector2()
	ctrlAPos.x=startPos.x+(startPos.x-endPos.x)*0.1
	ctrlAPos.y=endPos.y-(endPos.y-startPos.y)*0.2
	ctrlBPos.y=endPos.y+(endPos.y-startPos.y)*0.3
	ctrlBPos.x=startPos.x-(startPos.x-endPos.x)*0.3
	for i in range(20):
		var t=float(i)/19 
		var pos=startPos*(1-t)*(1-t)*(1-t)+3*ctrlAPos*t*(1-t)*(1-t)+3*ctrlBPos*t*t*(1-t)+endPos*t*t*t
		list[i].position=pos
	updateAngle()
func updateAngle():
	for i in range(20):
		if i==0:
			list[0].rotation_degrees=270
		else:
			var current=list[i]
			var last=list[i-1]
			var lenVec=current.position-last.position
			var a=lenVec.angle()
			#弧度转角度
			a=rad2deg(a)
			
			#var a=getAngleByVector(current.position-last.position)
			current.rotation_degrees=a
			


func getAngleByVector(vec):
	if vec.y==0 :
		if(vec.x < 0):
			return 270
		elif vec.x>0:
			return 90
		else:
			return 0
	if vec.x==0:
		if vec.y>=0:
			return 0
		elif vec.y < 0:
			return 180
	var tan_yx =abs(vec.y)/abs(vec.x);
	var angle = 0
	if vec.y > 0 && vec.x < 0:
		angle = 270 + atan(tan_yx)*180/PI;
	elif vec.y > 0 && vec.x > 0: 
		angle = 90 - atan(tan_yx)*180/PI;
	elif vec.y < 0 && vec.x < 0:
		angle = 270 -atan(tan_yx)*180/PI ;
	elif vec.y < 0 && vec.x > 0:
		angle = atan(tan_yx)*180/PI + 90;
	return angle
